package com.un.ebs.product.service.impl;

import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.OSSUtil;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.product.domain.ProductImage;
import com.un.ebs.product.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * <p>
 *
 * </p>
 *
 * @author liubo
 * @create 2021-04-29 11:29
 */
@Service
public class FileServiceImpl implements FileService {


    @Override
    public String uploadOss(MultipartFile file, String pathKey, String fileKey) {
        if (StringUtils.isNullOrEmpty(pathKey)) {
            throw new BusinessException("pathKey 不能为空。");
        }

        if (StringUtils.isNullOrEmpty(fileKey)) {
            fileKey = UUID.randomUUID().toString();
        }
        String url = OSSUtil.upload(file, String.format("entity_file/%s", pathKey), null, fileKey);
        return fileKey;

//        try {
//            // TODO: 2021/4/29 写缓存和索引
//        } catch (Exception e) {
//            OSSUtil.delete(url);
//            throw new BusinessException("上传失败：" + e.getMessage());
//        }
//        return url;
    }

    @Override
    public String getOssUrl(String key) {
        return OSSUtil.getObjectUrl(key).toString();
    }

    @Override
    public void deleteByKey(String key) {

    }
}
